using MicroMsgBackground.Common.Utils;
using MicroMsgBackground.Manager;
using MicroMsgBackground.Protocol;
using System;
using System.Threading;
namespace MicroMsgBackground.Network
{
	public class NetHandler
	{
		private const int STATUS_NULL = 0;
		private const int STATUS_RUNNING = 1;
		private const int STATUS_PAUSED = 2;
		private static int mRunStatus;
		public static void onApplicationDeactivated()
		{
			if (NetHandler.mRunStatus == 1)
			{
				NetHandler.mRunStatus = 2;
			}
		}
		public static void onApplicationActivated()
		{
			if (NetHandler.mRunStatus == 2)
			{
				SessionPackMgr.cleanAllTimeoutPoint(3);
				SessionPackMgr.markAllToNotSended(3);
				if (SessionPackMgr.getAuthStatus() == 1)
				{
					SessionPackMgr.setAuthStatus(0);
				}
				Connector.onPrepareSend(true);
				NetHandler.mRunStatus = 1;
			}
		}
		public static void smethod_0()
		{
			int num = 100;
			HostService.init();
			NetworkDeviceWatcher.initWatcher();
			while (NetHandler.mRunStatus != 0)
			{
				Thread.Sleep(num);
				if (NetHandler.mRunStatus == 1)
				{
					Sender.getInstance().checkSender();
					SessionPackMgr.checkPackTimeout();
					SessionPackMgr.clearCompletedOrCancel();
					SessionPackMgr.checkSendTimeout();
					Connector.checkReady();
					Sender.getInstance().handler();
					Receiver.getInstance().handler();
					num = NetHandler.getIdleTime();
				}
			}
		}
		public static void smethod_1(int MaxTimes = 0)
		{
			int num = 100;
			int num2 = 0;
			HostService.init();
			NetworkDeviceWatcher.initWatcher();
			while (NetHandler.mRunStatus != 0 && num2 < MaxTimes)
			{
				Thread.Sleep(num);
				num2++;
				if (NetHandler.mRunStatus == 1)
				{
					Sender.getInstance().checkSender();
					SessionPackMgr.checkPackTimeout();
					SessionPackMgr.clearCompletedOrCancel();
					SessionPackMgr.checkSendTimeout();
					Connector.checkReady();
					Sender.getInstance().handler();
					Receiver.getInstance().handler();
					num = NetHandler.getIdleTime();
				}
			}
		}
		public static int getIdleTime()
		{
			double now = SessionPack.getNow();
			double mLastActiveTimestamp = Connector.mLastActiveTimestamp;
			int num = (int)((now - mLastActiveTimestamp) / 100.0);
			if (num > 1000)
			{
				num = 1000;
			}
			if (num < 10)
			{
				num = 10;
			}
			return num;
		}
		public static void startup()
		{
			if (NetHandler.mRunStatus != 0)
			{
				return;
			}
			NetHandler.mRunStatus = 1;
			Log.smethod_4("Network", "network handler startup...." + AccountMgr.GetCurrentLanguage());
			Log.smethod_4("Network", "device id = " + Util.getDeviceUniqueId());
			Log.smethod_4("Network", "device type = " + ConstantsProtocol.DEVICE_TYPE);
			Log.smethod_4("Network", "version = 0x" + ConstantsProtocol.CLIENT_VERSION.ToString("x"));
		}
		private static void stop()
		{
			NetHandler.mRunStatus = 0;
		}
	}
}
